package com.qixin.modules.ea.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qixin.modules.ea.entity.EaPhoneRecord;
import com.qixin.modules.ea.service.IEaPhoneRecordService;
import com.qixin.modules.ea.vo.EaPhoneRecordPage;
import com.qixin.modules.ea.vo.EaPhoneRecordPrinfVo;
import com.qixin.modules.ea.vo.PhoneRecordPageVo;
import com.qixin.modules.eaApplication.entity.EaEntertainApply;
import com.qixin.modules.eaApplication.mapper.EaEntertainApplyMapper;
import com.qixin.modules.eaApplication.service.IEaEntertainApplyService;
import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import com.qixin.modules.eaBasic.vo.IdVo;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.EmptyUtil;
import org.jeecg.modules.activity.service.ActivityCommonService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
* @Description: ea_phone_record
* @Author: qixin-boot
* @Date:   2021-11-03
* @Version: V1.0
*/
@Api(tags="电话记录单")
@RestController
@RequestMapping("/ea/eaPhoneRecord")
@Slf4j
public class EaPhoneRecordController extends JeecgController<EaPhoneRecord, IEaPhoneRecordService> {
   @Autowired
   private IEaPhoneRecordService eaPhoneRecordService;
   @Autowired
   private ChangeIdToNameUtil changeIdToNameUtil;
   @Autowired
   private EaEntertainApplyMapper eaEntertainApplyMapper;
   // 工作流公共执行类
   @Autowired
   private ActivityCommonService activityCommonService;

    /**
     * 查询电话记录单信息列表
     *
     * @param phoneRecordPageVo
     * @return Result
     */
    @ApiOperation(value="电话记录单信息列表-查询", notes="电话记录单信息列表-查询")
    @PostMapping(value = "/list")
    public Result<?> list(@RequestBody PhoneRecordPageVo phoneRecordPageVo) {
        Map<String,Object> resutlList = eaPhoneRecordService.list(phoneRecordPageVo);
        return Result.OK(resutlList);
    }

    /**
     *  编辑
     *
     * @param eaPhoneRecordPage
     * @return
     */
    @AutoLog(value = "电话记录单-编辑")
    @ApiOperation(value="电话记录单-编辑", notes="电话记录单-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody EaPhoneRecordPage eaPhoneRecordPage) {
        EaPhoneRecord eaPhoneRecord = new EaPhoneRecord();
        BeanUtils.copyProperties(eaPhoneRecordPage, eaPhoneRecord);
        EaPhoneRecord eaPhoneRecordEntity = eaPhoneRecordService.getById(eaPhoneRecord.getId());
        if(eaPhoneRecordEntity==null) {
            return Result.error("未找到对应数据");
        }

        //判断单据是否已经被提交
        EaPhoneRecord eaPhoneRecord1 = eaPhoneRecordService.getById(eaPhoneRecordPage.getId());
        String status = EmptyUtil.checkNull(eaPhoneRecord1.getBillStatus());   //当状态为1审批中,2已审批时,提示单据状态已变更,请刷新
        if(status.equals("1") || status.equals("2")){
            return Result.error("单据状态已变更,请刷新");
        }

        eaPhoneRecord.setSaveType("add");    //保存
        Map<String,Object> returnMap = eaPhoneRecordService.updateMain(eaPhoneRecord);

        if(returnMap.get("code").equals("-1")){
            return Result.error(EmptyUtil.checkNull(returnMap.get("message")));
        }else{
            returnMap.remove("code");
            returnMap.remove("message");
            return Result.OK(returnMap);
        }
    }

    /**
     *   通过id删除
     *
     * @param idVo
     * @return
     */
    @AutoLog(value = "电话记录单-通过id删除(未提交状态可以删除)")
    @ApiOperation(value="电话记录单-通过id删除", notes="电话记录单-通过id删除")
    @PostMapping(value = "/delete")
    public Result<?> delete(@RequestBody IdVo idVo) {
        String id = EmptyUtil.checkNull(idVo.getId());
        EaPhoneRecord eaPhoneRecordEntity = eaPhoneRecordService.getById(id);
        if(eaPhoneRecordEntity==null) {
            return Result.error("未找到对应数据");
        }
        Map<String,Object> returnMap = eaPhoneRecordService.delMain(id);
        if(returnMap.get("code").equals("-1")){
            String message = EmptyUtil.checkNull(returnMap.get("message"));
            return Result.error(message);
        }else{
            return Result.OK("删除成功!");
        }
    }

    /**
     * 通过id查询
     *
     * @param idVo
     * @return
     */
    @ApiOperation(value="电话记录单-通过id查询", notes="电话记录单-通过id查询")
    @PostMapping(value = "/queryById")
    public Result<?> queryById(@RequestBody IdVo idVo) {
        String id = EmptyUtil.checkNull(idVo.getId());
        EaPhoneRecord eaPhoneRecord = eaPhoneRecordService.getById(id);
        if(eaPhoneRecord==null) {
            return Result.error("未找到对应数据");
        }
        Map<String, Object> resutlList = eaPhoneRecordService.selectById(id);
        return Result.OK(resutlList);
    }

    @ApiOperation(value="电话记录单-通过招待费id查询", notes="电话记录单-通过招待费id查询")
    @GetMapping(value = "selectByApplyId")
    public Result<?> selectByApplyId(@RequestParam(name="applyId",required=true) String applyId ) {
        EaPhoneRecord eaPhoneRecord = eaPhoneRecordService.selectByApplyId(applyId);
        if(eaPhoneRecord != null){
            String acceptDept = eaPhoneRecord.getAcceptDept();
            String acceptDeptName = changeIdToNameUtil.getNameByDepartId(acceptDept);
            eaPhoneRecord.setAcceptDeptName(acceptDeptName);
        }
        return Result.OK(eaPhoneRecord);
    }

    /**
    * @Description 电话记录单打印模板-通过招待费id查询
    * @author  wujinbiao
    * @date 2022/2/8
    * @param id
    * @return org.jeecg.common.api.vo.Result<?>
    */
    @ApiOperation(value="电话记录单打印模板-通过招待费id查询", notes="电话记录单打印模板-通过招待费id查询")
    @GetMapping(value = "/selectByMainid")
    public Result<?> selectById(@RequestParam(name="id",required=true) String id ) {
        EaPhoneRecord eaPhoneRecord = eaPhoneRecordService.selectByApplyId(id);
        if (eaPhoneRecord == null) {
            return Result.error("未找到对应数据");
        }
        EaPhoneRecordPrinfVo eaPhoneRecordPrinfVo = new EaPhoneRecordPrinfVo();
        BeanUtils.copyProperties(eaPhoneRecord,eaPhoneRecordPrinfVo);


        //审批流程
        // update-begin-Autor: wjb date: 2022-02-10 14:50 for: 添加流程审批记录
        EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectById(id);
        if (StringUtils.isNotBlank(eaEntertainApply.getProcInstId())){
            // 获取招待费申请单据编号、单据日期
            eaPhoneRecordPrinfVo.setBillNo(eaEntertainApply.getBillNo());
            eaPhoneRecordPrinfVo.setBillDate(eaEntertainApply.getBillDate());

            List<Map<String, Object>> nodeList = activityCommonService.getNodeList(eaEntertainApply.getProcInstId());
            eaPhoneRecordPrinfVo.setSpjl(nodeList);
        }
        // update-end-Autor: wjb date: 2022-02-10 14:50 for: 添加流程审批记录

        // 获取部门负责人、综合办公室 审批意见
        if (StringUtils.isNotBlank(eaEntertainApply.getProcInstId())){
            List<Map<String, Object>> nodeList = activityCommonService.getNodeListDesc(eaEntertainApply.getProcInstId());
            for (Map node:nodeList) {
                String nodeName = (String) node.get("node_name");
                if("综合办公室".equals(nodeName)){
                    eaPhoneRecordPrinfVo.setZhbgs((String) node.get("audit_comment"));
                    break;
                }
            }
            for (Map node:nodeList) {
                String nodeName = (String) node.get("node_name");
                if("部门负责人".equals(nodeName)){
                    eaPhoneRecordPrinfVo.setBmfzr((String) node.get("audit_comment"));
                    break;
                }
            }
        }
        return Result.OK(eaPhoneRecordPrinfVo);
    }

    /**
     * 电话记录单审批/退回
     *
     * @param eaTaskVo
     * @return
     */
    @AutoLog(value = "电话记录单审批/退回")
    @ApiOperation(value="电话记录单审批/退回", notes="电话记录单审批/退回")
    @PostMapping(value = "/audit")
    public Result<?> edit(@RequestBody EaTaskVo eaTaskVo) {
        Map<String,Object> returnMap = eaPhoneRecordService.audit(eaTaskVo);
        return Result.OK(returnMap);
    }

   /**
   * 导出excel
   *
   * @param request
   * @param eaPhoneRecord
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, EaPhoneRecord eaPhoneRecord) {
       return super.exportXls(request, eaPhoneRecord, EaPhoneRecord.class, "ea_phone_record");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, EaPhoneRecord.class);
   }

}
